(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:PickpointCtrl
     * @description
     * # PickpointCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('PickpointCtrl', PickpointCtrl);

    PickpointCtrl.$inject = ['$scope', '$rootScope', '$state', 'dataService', '$location', 'helper', '$timeout', 'sessionService', 'cacheService', 'ERP_CONFIG', '$compile', 'moment', 'base64'];

    function PickpointCtrl($scope, $rootScope, $state, dataService, $location, helper, $timeout, sessionService, cacheService, ERP_CONFIG, $compile, moment, base64) {
        $scope.listAction = function() {
            $scope.pageSize = 10;
            $scope.getpickpointList();
            $scope.$on('event:up-store-id', function() {
                $scope.getpickpointList();
            });
        }

        //获取发货点列表
        $scope.getpickpointList = function(pageNo) {
            var query = {
                content: {
                    page_no: pageNo ? pageNo : "1",
                    page_size: $scope.pageSize,
                    store_id: $rootScope.store_id,
                    all: 1
                }
            };

            dataService.pickpointGet(query, true).then(function(list) {
                if (list && list.pickpoint_list) {
                    $scope.pickpoint_list = list.pickpoint_list;
                    $scope.pick_total = list.total_count;
                }
            });
        }

        //发货点添加、编辑页面
        $scope.addAction = function() {
            var pickpoint_id = $location.search().pickpoint_id;
            if (pickpoint_id) {
                var query = {
                    content: {
                        pickpoint_id: pickpoint_id,
                    }
                };

                dataService.pickpointGet(query, true).then(function(list) {
                    if (list && list.pickpoint_list[0]) {
                        $scope.pickpoint = list.pickpoint_list[0];
                        if($scope.pickpoint.p_pickpoint_id){
                            $scope.pickpoint.p_pickpoint_id = Number($scope.pickpoint.p_pickpoint_id);
                        }
                    }
                });
            } else {
                $scope.pickpoint = {
                    store_id: $rootScope.store_id,
                    province: "",
                    city: "",
                    district: "",
                    province_id: 0,
                    city_id: 0,
                    district_id: 0,
                    address: "",
                    phone: "",
                    p_pickpoint_id:0,
                    delivery_name: ""
                };
            }
        }

        //地图选点回调函数
        $scope.addresCallback = function(data, jingweidu) {
            $timeout(function() {
                if (data.address) {
                    $scope.pickpoint.address = data.address;
                    $scope.pickpoint.longitude = data.localtion.lng;
                    $scope.pickpoint.latitude = data.localtion.lat;
                }
            });
        }

        //省份级联选择
        $scope.getCity = function(data) {
            $scope.cityData = data;
            if (data && data[0] && data[0].id && data[1] && data[1].id && data[2] && data[2].id) {
                $scope.pickpoint.province = data[0].name;
                $scope.pickpoint.city_id = data[1].id;
                $scope.pickpoint.city = data[1].name;
                $scope.pickpoint.district_id = data[2].id;
                $scope.pickpoint.district = data[2].name;
            }
        }

        $scope.pickpointAdd = function() {
            if ($scope.pickpoint) {
                if (!$scope.pickpoint.province && !$scope.pickpoint.city && !$scope.pickpoint.district) {
                    helper.myAlert("省市地区没有填写，不能保存发货点！");
                    return false;
                }

                if ($scope.pickpoint.pickpoint_id) {
                    var data = {
                        content: $scope.pickpoint
                    };
                    dataService.pickpointUpdate(data).then(function(re) {
                        if (re.pickpoint_id) {
                            helper.myAlert("success", "#/pickpoint/list");
                        } else {
                            helper.myAlert(re.Msg);
                        }
                    });
                } else {
                    var data = {
                        content: $scope.pickpoint
                    };
                    dataService.pickpointUpdate(data).then(function(re) {
                        if (re.pickpoint_id) {
                            helper.myAlert("success", "#/pickpoint/list");
                        } else {
                            helper.myAlert(re.errmsg);
                            return false;
                        }
                    });
                }
            } else {
                helper.myAlert("发货点数据不存在！");
            }
        };

        //发货点删除
        $scope.deletePick = function(pickpoint_id) {
            if (pickpoint_id) {
                helper.myConfirm('确认删除吗？', function() {
                    var query = {
                        content: {
                            pickpoint_id: pickpoint_id
                        }
                    };

                    dataService.pickpointDelete(query).then(function(result) {
                        if (result.Msg == "ok") {
                            helper.myAlert("success", null, null, function() {
                                $state.go("pickpoint.list", null, {
                                    reload: true
                                });
                            });
                        } else {
                            helper.myAlert("error：" + result.Msg);
                            return false;
                        }
                    });
                });
            }
        }

        //status=1启用，0不启用
        $scope.statusUpdate = function(status, list, $event) {
            var data = list;
            data.status = status;
            var xdata = {
                "content": data
            };

            dataService.pickpointStatusUpdate(xdata).then(function(r) {
                if (r.pickpoint_id) {
                    helper.myTip("success", $event.currentTarget);
                    $timeout(function() {
                        list.status = status;
                    });
                } else {
                    helper.myAlert("error：" + r.Msg);
                    return false;
                }
            });
        }
    }
})();